package com.edol.painting.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.edol.painting.R;

/*  =======================================================================================
 *  Copyright (C) 2017 dlboard.com. All rights reserved.
 *
 *  This computer program source code file is protected by copyright law and international
 *  treaties. Unauthorized distribution of source code files, programs, or portion of the
 *  package, may result in severe civil and criminal penalties, and will be prosecuted to
 *  the maximum extent under the law.
 *
 *  ---------------------------------------------------------------------------------------
 * Author:Administrator
 * Date:2017/7/20
 * Desc:
 *
 * ======================================================================================== */

public class ProgressHUD {

    private ProgressDialog progressDialog;
    private Context mContext;
    private String mLoadDesc;

    private ProgressHUD(Context context) {
        progressDialog = new ProgressDialog(context);
        mContext = context;
    }

    /**
     * 创建 ProgressHUD
     * @param context
     * @return
     */
    public static ProgressHUD create(Context context) {
        return new ProgressHUD(context).setCancellable(false);
    }

    public ProgressHUD setCancellable(boolean isCancellable) {
        progressDialog.setCancelable(isCancellable);
        return this;
    }


    public ProgressHUD show() {
        if (!isShowing()) {
            progressDialog.show();
        }
        return this;
    }

    public boolean isShowing() {
        return progressDialog != null && progressDialog.isShowing();
    }

    public void dismiss() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    private class ProgressDialog extends Dialog {

        public ProgressDialog(Context context) {
            super(context);
        }


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            requestWindowFeature(Window.FEATURE_NO_TITLE);
            View view = LayoutInflater.from(mContext).inflate(R.layout.layout_progress, null);
            setContentView(view);
            if (!TextUtils.isEmpty(mLoadDesc)) {
                TextView descTv = (TextView) view.findViewById(R.id.load_desc);
                descTv.setText(mLoadDesc);
            }

            Window window = getWindow();
            window.setBackgroundDrawable(new ColorDrawable(0));
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.dimAmount = 0.5f;
            layoutParams.gravity = Gravity.CENTER;
            window.setAttributes(layoutParams);
            setCanceledOnTouchOutside(false);
        }
    }

    public void setLoadDesc(String desc) {
        mLoadDesc = desc;
    }
}
